﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Workflow.Interface;
using MyLib;
using Workflow.Entity;
using System.Collections;
using System.Data;

namespace Workflow
{
    public partial class DraftList : System.Web.UI.Page
    {
        IProcessCategory _category = ServiceContainer.Instance().GetService<IProcessCategory>();
        ITask _task = ServiceContainer.Instance().GetService<ITask>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtType.Text = Request.QueryString["Type"];
                txtSort.Text = Request.QueryString["Sort"];
                txtPreSort.Text = Request.QueryString["Sort"];
                txtDateType.Text = Request.QueryString["DateType"];
                txtProcessCategory.Text = Request.QueryString["ProcessCategory"];
                if (txtProcessCategory.Text == Workflow.Resources.lang.TaskList_All)
                {
                    txtProcessCategory.Text = "";
                }
                txtProcessName.Text = Request.QueryString["ProcessName"];
                txtIncident.Text = Request.QueryString["Incident"];
                txtStartDate.Text = Request.QueryString["StartDate"];
                txtEndDate.Text = Request.QueryString["EndDate"];
                txtApplicant.Text = Request.QueryString["Applicant"];
                txtSummary.Text = Request.QueryString["Summary"];
                txtShowQuery.Text = Request.QueryString["ShowQuery"];
                BindProcessCategory();
                BindGrid();

                lblProcessName.Text = Workflow.Resources.lang.TaskList_ProcessName;
                lblStartTime.Text = Workflow.Resources.lang.TaskList_StartTime;
                lblIncident.Text = Workflow.Resources.lang.TaskList_Incident;
                lblApplicant.Text = Workflow.Resources.lang.TaskList_Applicant;
                lblSummary.Text = Workflow.Resources.lang.TaskList_Summary;
                //btnSearch.Text = Workflow.Resources.lang.TaskList_Search;
                AspNetPager1.FirstPageText = Workflow.Resources.lang.FirstPage;
                AspNetPager1.PrevPageText = Workflow.Resources.lang.PrevPage;
                AspNetPager1.NextPageText = Workflow.Resources.lang.NextPage;
                AspNetPager1.LastPageText = Workflow.Resources.lang.LastPage;
            }
        }

        void BindProcessCategory()
        {
            List<ProcessCategoryEntity> lists = _category.GetList();
            ProcessCategoryEntity ety = new ProcessCategoryEntity();
            ety.CATEGORYNAME = Workflow.Resources.lang.TaskList_All;
            ety.CATEGORYID = "0";
            lists.Insert(0,ety);
            //rptProcessCategory.DataSource = lists;
            //rptProcessCategory.DataBind();
        }

        void BindGrid()
        {
            DataTable dt = new DataTable();
            dt = DataAccess.Instance("BizDB").ExecuteDataTable("select * from WF_DRAFT where createby='"+Page.User.Identity.Name+"'");
            rptTask.DataSource = dt;
            rptTask.DataBind();
        }

        protected void lbProcessCategory_Click(object sender, EventArgs e)
        {
            LinkButton linkButton = (LinkButton)sender;
            string id = linkButton.CommandArgument;
            if (id == Workflow.Resources.lang.TaskList_All)
            {
                txtProcessCategory.Text = "";
            }
            else
            {
                txtProcessCategory.Text = id;
            }
            BindGrid();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindGrid();

        }

        protected void rptTask_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                string str = e.CommandArgument.ToString();
                DataAccess.Instance("BizDB").ExecuteNonQuery("delete from WF_DRAFT where formid='"+str+"'");
                BindGrid();
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + Portal.Resources.lang.SecurityList_DeleteSuccess + "!');", true);
            }
        }

        //public string GetUrl(string formId,string tableName)
        //{
        //    tableName = tableName.Split(',')[0];
        //    DataTable dt = new DataTable();
        //    dt = DataAccess.Instance("BizDB").ExecuteDataTable("select * from "+tableName+" where FORMID='" + formId + "'");
        //    if (dt.Rows.Count > 0)
        //    {
        //        return "OpenForm.aspx?ProcessName=" + dt.Rows[0]["ProcessName"].ToString() + "&Incident=" + dt.Rows[0]["Incident"].ToString();
        //    }

        //    return "";
        //}

        public string GetUrl(object obj)
        {
            return Server.UrlEncode(obj.ToString());
        }
    }
}